package com.dyuloon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dyuloon.entity.Client;
import com.dyuloon.entity.ConsumeClient;
import com.dyuloon.entity.RechargeClient;
import com.dyuloon.from.SearchForm;
import com.dyuloon.mapper.SaveMoneyMapper;
import com.dyuloon.service.SaveMoneyService;
import com.dyuloon.util.PageVOUtil;
import com.dyuloon.util.ResultVOUtil;
import com.dyuloon.vo.PageVO;
import com.dyuloon.vo.ResultVO;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class SaveMoneyServiceImpl extends ServiceImpl<SaveMoneyMapper, Client> implements SaveMoneyService {
    @Autowired(required = false)
    private SaveMoneyMapper saveMoneyMapper;

    @Override
    public PageVO selectClient(SearchForm searchForm) {//查询客户
        Page clientPage = new Page(searchForm.getPageNum(), searchForm.getPageSize());
        //id不为空?
        QueryWrapper<Client> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(Strings.isNotEmpty(searchForm.getValue()), searchForm.getKey(), searchForm.getValue());
        this.saveMoneyMapper.selectPage(clientPage, queryWrapper.orderByDesc("id"));
        PageVO pageVO = PageVOUtil.success(clientPage.getRecords(), "查询成功！", clientPage.getTotal(), clientPage.getCurrent(), clientPage.getSize());
        return pageVO;
    }

    @Override
    public ResultVO openAccount(Client client) {//开户
        int save = this.saveMoneyMapper.insert(client);
        ResultVO resultVO = save == 1 ? ResultVOUtil.success(null, "添加成功！") : ResultVOUtil.fail("添加失败！");
        return resultVO;
    }

    @Override
    public ResultVO recharge(RechargeClient rechargeClient) {//充值
        Client client = new Client();
        ResultVO resultVO = null;
        //验证充值金额的有效性
        if (rechargeClient.getBalance() <= 0) {
            resultVO = ResultVOUtil.fail("充值金额必须大于0！");
            return resultVO;
        }
        BeanUtils.copyProperties(rechargeClient, client);
        if (this.saveMoneyMapper.recharge(client)) {
            resultVO = ResultVOUtil.success(null, "更新成功！");
        } else {
            resultVO = ResultVOUtil.fail("更新失败！");
        }
        return resultVO;
    }

    @Override
    public ResultVO consume(ConsumeClient consumeClient) {//消费
        Client client = new Client();
        ResultVO resultVO = null;
        //验证充值金额的有效性
        if (consumeClient.getBalance() <= 0) {
            resultVO = ResultVOUtil.fail("消费金额必须大于0！");
            return resultVO;
        }
        //验证密码是否正确
        if (!consumeClient.getPassword().equals(this.saveMoneyMapper.selectPassword(consumeClient.getId()))) {
            resultVO = ResultVOUtil.fail("账号或密码错误！");
            return resultVO;
        }
        //验证客户账户余额是否充足
        if (this.saveMoneyMapper.selectBalance(consumeClient.getId()) < consumeClient.getBalance()) {
            resultVO = ResultVOUtil.fail("余额不足！");
            return resultVO;
        }
        BeanUtils.copyProperties(consumeClient, client);
        if (this.saveMoneyMapper.consume(client)) {
            resultVO = ResultVOUtil.success(null, "消费成功！");
        } else {
            resultVO = ResultVOUtil.fail("更新失败！");
        }
        return resultVO;
    }
}
